﻿using System;
using System.Collections.Generic;

using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>
    /// Generic component which provides values for the <see cref="ISearchEngine"/> object's filter.
    /// </summary>
    /// <remarks>Commonly used in pair with a <see cref="ISearchView"/> component.</remarks>
    public interface ISearchFilter
    {
        #region Methods.

        /// <summary>
        /// Gets <see cref="ISearchFilter"/> settings.
        /// </summary>
        /// <returns>A collection of key-value property pairs guaranteed to work only with 
        /// the <see cref="ISearchFilter"/> implementation which created it.</returns>
        IDictionary<string, object> GetSettings();

        /// <summary>
        /// Applies <see cref="ISearchFilter"/> settings to the current instance.
        /// </summary>
        /// <param name="properties">Settings collection which was created by the same or compatibile
        /// <see cref="ISearchFilter"/> implementation as the current instance.</param>
        void ApplySettings(IDictionary<string, object> properties);

        /// <summary>
        /// Orders the SearchFilter to write its internal state and entered values into engine's search fields.
        /// </summary>
        void WriteToSearchFields();

        /// <summary>
        /// Orders the SearchFilter to write its internal state and entered values into engine's search fields.
        /// </summary>
        /// <param name="overwriteAutoPopulatedFields">Indicates whether the search fields whose IsAutoPopulated property is set to true may be overwritten.</param>
        /// <param name="overwriteHiddenFields">Indicates whether the search fields whose IsHidden property is set to true may be overwritten.</param>
        void WriteToSearchFields(bool overwriteAutoPopulatedFields, bool overwriteHiddenFields);

        /// <summary>
        /// Orders the SearchFilter to set its internal state and display values so that 
        /// it matches the values stored in the engine's search fileds.
        /// </summary>
        void ReadSearchFields();

        #endregion

        #region Properties.

        /// <summary>
        /// Gets or sets the underlying search engine which contains meta-data and accesses the data-source.
        /// </summary>
        ISearchEngine Engine { get; set; }

        /// <summary>
        /// Occurs when the filter performs one or more actions. Eg. changes filter.
        /// </summary>
        event EventHandler<SearchFilterCommandEventArgs> FilterCommand;

        /// <summary>
        /// Gets or sets the <see cref="IMessageFormatter"/> object used to format values.
        /// </summary>
        IMessageFormatter Formatter { get; set; }

        /// <summary>
        /// Gets or sets a named collection of serialized filter states (see <see cref="SearchEngineFieldsState"/>) 
        /// compatibile with the provided <see cref="Engine"/>.
        /// </summary>
        IDictionary<string, string> PredefinedSearchEngineFieldsStates { get; set; }

        #endregion
    }
}